<template>
  <div class="fertilizer-container">
    <h2>{{item}}</h2>
    <div class="chart" id="chart_right2"></div>
  </div>
</template>

<script>
export default {
  name: "fertilizer",
  data() {
    return {
      item: "芽前肥",
      myChart: null,
    };
  },
  computed: {
    dataPageData() {
      return this.$store.state.dataPageData;
    },
  },
  watch: {
    dataPageData: {
      immediate: true,
      deep: true,
      handler(res) {
        if (res) {
          // 看数据格式再填入参数
          this.getEchart(res.datas);
        }
      },
    },
  },
  mounted() {
    this.getEchart();
  },
  methods: {
    getEchart(datas) {
      if (this.myChart) {
        this.myChart.dispose();
        this.myChart = null;
      }
      this.myChart = echarts.init(document.getElementById("chart_right2"));
      let option = {
        title: {
          left: "center",
        },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)",
        },
        // legend: {
        //   orient: "vertical",
        //   left: "left",
        // },
        series: [
          {
            name: "施肥占比",
            type: "pie",
            radius: "50%",
            data: datas || [
              { value: 1048, name: "农家肥" },
              { value: 735, name: "尿素" },
              { value: 580, name: "过磷酸钙" },
              { value: 484, name: "硫酸钾" },
              { value: 300, name: "复合肥" },
            ],
            emphasis: {
              itemStyle: {
                // shadowBlur: 10,
                // shadowOffsetX: 0,
                // shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      };
      if (this.myChart) {
        this.myChart.clear();
        this.myChart.setOption(option, true);
      }
      // myChart.setOption(option, true);
      window.addEventListener("resize", () => {
        this.myChart.resize();
      });
    },
  },
  beforeDestroy() {},
};
</script>

<style lang="scss" scoped>
.fertilizer-container {
  h2 {
    text-align: left;
    font-size: 0.2rem;
    color: white;
  }
  .chart {
    height: 3rem;
  }
}
</style>
